package com.lianzai.common.utils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.Data;

/**
 * Created by ZHOUZHOU on 17/8/7.
 */
@Data
public class BaseResult<T> {

  @JsonView(BaseResultView.class)
  private int code;
  @JsonView(BaseResultView.class)
  private String msg;
  @JsonView(BaseResultView.class)
  private T data;

  public static interface BaseResultView {

  }
  
  
  @JsonIgnore
  public boolean isSuccess() {
	  return ResultCode.SUCCESS.getCode() == this.code ;
  }
  
  
  @JsonIgnore
  public boolean isFail() {
	  return ResultCode.SUCCESS.getCode() != this.code ;
  }

  public BaseResult(T data) {
    this.code = ResultCode.SUCCESS.getCode();
    this.msg = ResultCode.SUCCESS.getMsg();
    this.data = data;
  }

  public BaseResult() {
    this.code = ResultCode.SUCCESS.getCode();
    this.msg = ResultCode.SUCCESS.getMsg();
  }

  public BaseResult(int code, String msg) {
    this.code = code;
    this.msg = msg;
  }

  public static BaseResult fail() {
    return fail(ResultCode.FAIL.getCode(), ResultCode.FAIL.getMsg());
  }

  public static <T> BaseResult<T> fail(String msg) {
    return fail(ResultCode.FAIL.getCode(), msg);
  }

  public static <T> BaseResult<T> fail(int code, String msg) {
    BaseResult<T> r = new BaseResult<>();
    r.setCode(code);
    r.setMsg(msg);
    return r;
  }

  public static <T> BaseResult<T> success() {
    return new BaseResult<>();
  }

  public static <T> BaseResult<T> success(T t) {
    BaseResult<T> r = new BaseResult<>();
    r.setData(t);
    return r;
  }

}
